package domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.xml.bind.annotation.XmlRootElement;

//@SuppressWarnings("serial")
@XmlRootElement
@Entity
public class AccUser implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String name;
        private String email;
        private String rights;
        private String password;
        private String Salt;
        @OneToMany
        @OrderBy("name asc") 
        private List<Author> authors;
        @OneToMany
        @OrderBy("title asc")   
        private List<Reference> references;
        @OneToMany
        @OrderBy("name asc")  
        private List<AcademicSet> publications;
        @OneToMany
        @OrderBy("name asc")
        private List<Format> formats; 

        public List<AcademicSet> getPublications() {
                return publications;
        }

        public void setPublications(List<AcademicSet> publications) {
                this.publications = publications;
        }
        
        public void deletePublications(long id) {
                for (AcademicSet aa : publications) {
                        if (aa.getId() == id) {
                                publications.remove(aa);
                                break;
                        }
                }
        }
        public void deleteFormat(long id) {
                for (Format aa : formats) {
                        if (aa.getId() == id) {
                                formats.remove(aa);
                                break;
                        }
                }
        }
        

        public void deleteReference(int id) {
            
          
            
            Reference r = new Reference();
               for (Reference aa : references) {
                        if (aa.getId() == id) {
                                //references.remove(aa);
                            //break;
                            r = aa;
                        }
                }
               references.remove(r);
        }

        public List<Author> getAuthors() {
                return authors;
        }

        public void setAuthors(List<Author> authors) {
                this.authors = authors;
        }

        public void addAuthor(Author auth) {
                this.authors.add(auth);
        }

        public void deleteAuthor(int id) {
                for (Author aa : authors) {
                        if (aa.getId() == id) {
                                authors.remove(aa);
                                break;
                        }
                }
        }

        public void addReference(Reference ref) {
                this.references.add(ref);
        }

        public List<Reference> getRefs() {
                return references;
        }

        public void addAcademicSet(AcademicSet acs) {
                if (!getPublications().contains(acs)) {
                        this.publications.add(acs);
                }
        }

        public void addFormat(Format form) {
                this.formats.add(form);
        }

        public String getSalt() {
                return Salt;
        }

        public void setSalt(String Salt) {
                this.Salt = Salt;
        }

        public AccUser(String name, String email, String rightd, String password) {
                this.id = id;
                this.name = name;
                this.email = email;
                this.rights = rightd;
                this.password = password;
                authors = new ArrayList<Author>();
                references = new ArrayList<Reference>();
                publications = new ArrayList<AcademicSet>();
                formats = new ArrayList<Format>();

        }

        @Override
        public String toString() {
                return "AccUser{" + "id=" + id + ", name=" + name + ", email=" + email + ", rightd=" + rights + ", password=" + password + '}';
        }

        public String getPassword() {
                return password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        public AccUser() {
        }

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getEmail() {
                return email;
        }

        public void setEmail(String email) {
                this.email = email;
        }

        public String getRights() {
                return rights;
        }

        public void setRights(String rights) {
                this.rights = rights;
        }

    public List<Format> getFormats() {
        return formats;
    }

    public void setFormats(List<Format> formats) {
        this.formats = formats;
    }
        
}
